<?php

namespace app\controller\admin\wzg;

use app\model\admin\wz\Wzhero;
use app\model\admin\wz\WzheroMatch;
use app\model\admin\wzg\WzGetHero;
use app\model\admin\wzg\WzGetHeroEquip;
use app\model\admin\wzg\WzGetHeroRelation;
use app\model\admin\wzg\WzGetHeroRune;
use app\model\admin\wzg\WzGetHeroSkill;
use app\model\admin\wzg\WzGetHeroTeam;
use app\model\admin\wzg\WzGetRune;
use http\Client;
use laytp\controller\Backend;
use laytp\library\CommonFun;

/**
 * Class Game
 * @package app\controller\admin\wzg
 */
class Hero extends Backend
{
    /**
     * api_log模型对象
     * @var \app\model\api\Log
     */
    protected $model;
    protected $hasSoftDel = 0;//是否拥有软删除功能

    protected $noNeedLogin = []; // 无需登录即可请求的方法
    protected $noNeedAuth = ['index', 'info']; // 无需鉴权即可请求的方法

    public $orderRule = ['id' => 'asc'];

    public function _initialize()
    {
        $this->model = new WzGetHero();
    }

    //查看详情
    public function info()
    {
        $id   = $this->request->param('id');
        $info = $this->model->find($id);
        if(is_object($info)){
            $info = $info->toArray();
        }
        $info['relation'] = (new WzGetHeroRelation())->with("hero")->where("relation_hero_id","=", $info['relation_id'])->select()->toArray();
        $info['team'] = (new WzGetHeroTeam())->with(["hero","heroTwo"])->where("relation_hero_id","=", $info['relation_id'])->select()->toArray();
        $info['equipment'] = (new WzGetHeroEquip())->with(["equipment"])->where("relation_hero_id","=", $info['relation_id'])->select()->toArray();
        $info['rune'] = (new WzGetHeroRune())->where("hero_relation_id","=", $info['relation_id'])->select()->toArray();
        foreach ($info['equipment'] as &$item){
            $item['show_rate'] = sprintf("%.2f",$item['show_rate']*100)."%";
            $item['win_rate'] = sprintf("%.2f",$item['win_rate']*100)."%";
        }
        foreach ($info['team'] as &$item){
            $item['show_rate'] = sprintf("%.2f",$item['show_rate']*100)."%";
            $item['win_rate'] = sprintf("%.2f",$item['win_rate']*100)."%";
            $item['param_data'] = sprintf("%.2f",$item['param_data']);
        }
        foreach ($info['relation'] as &$item){
            $item['param_data'] = sprintf("%.2f",$item['param_data']*100);
        }
        $runesIds = [];
        foreach ($info['rune'] as &$item){
           $item['inscriptions'] = json_decode($item['inscriptions'],true);
            $item['show_rate'] = sprintf("%.2f",$item['show_rate']*100)."%";
            $item['win_rate'] = sprintf("%.2f",$item['win_rate']*100)."%";
           foreach ($item['inscriptions'] as $it){
               $runesIds[] = $it['runeId'];
           }
        }
        if(count($runesIds)>0){
            $runes = (new WzGetRune())->where("relation_id","in",array_unique($runesIds))->column("*","relation_id");
            foreach ($info['rune'] as &$item){
                $item['showLabel'] = [];
                foreach ($item['inscriptions'] as $it){
                    if(isset($runes[$it['runeId']])){
                        $item['showLabel'][] = $runes[$it['runeId']]['name']."*".$it['num'];
                    }
                }
                $item['showLabel'] = implode("；",$item['showLabel']);
            }
        }
        $info['skill'] = (new WzGetHeroSkill())->with("improvement")->where("hero_relation_id",'=',$info['relation_id'])->select()->toArray();
        foreach ($info['skill'] as &$item){
            $item['level'] = [];
            foreach ($item['improvement'] as $it){
                $data = [];
                if(!empty($it['one'])){
                    $data[] = $it['one'];
                }
                if(!empty($it['two'])){
                    $data[] = $it['two'];
                }
                if(!empty($it['three'])){
                    $data[] = $it['three'];
                }
                if(!empty($it['four'])){
                    $data[] = $it['four'];
                }
                if(!empty($it['five'])){
                    $data[] = $it['five'];
                }
                if(!empty($it['six'])){
                    $data[] = $it['six'];
                }
                if(count($data)>0){
                    $item['level'][] = $it['name']." ".implode(" / ",$data);
                }
            }
            $item['level'] = implode("<br/>",$item['level']);
        }
        return $this->success('获取成功', $info);
    }

}